﻿<%@ WebHandler Language="C#" Class="instructionsupload" %>

using System;
using System.Web;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;




public class instructionsupload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Headers.Add("connection", "keep-alive");
        context.Response.CacheControl = "no-cache";
        context.Response.AddHeader("Connection", "Keep-Alive");
        context.Response.Headers.Set("Connection", "Keep-Alive");
        context.Response.Headers.Add("Connection", "Keep-Alive");
        context.Response.AppendHeader("Connection", "Keep-Alive");
        SaveFile();  
    }

    
    MsSqlHelper mssql = new MsSqlHelper();
    Common Init = new Common();

    /// <summary>
    /// 文件保存操作
    /// </summary>
    /// <param name="basePath"></param>
    private void SaveFile(string basePath = "~/Upload/Images/")
    {
        var name = string.Empty;
        string fullbasePath = (basePath.IndexOf("~") > -1) ? HttpContext.Current.Server.MapPath(basePath) :
        fullbasePath = basePath;
        HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpPostedFile file;
        string folderdt = DateTime.Now.ToString("yyyy'/'MM'/'dd");
        if (!Directory.Exists(fullbasePath))
            Directory.CreateDirectory(fullbasePath);
        if (!Directory.Exists(basePath + folderdt + "\\"))
            Directory.CreateDirectory(fullbasePath + folderdt + "\\");
        
        string f = "00000000";
        for (int i = 0; i < HttpContext.Current.Request.Files.Count; ++i)
        {
            file = HttpContext.Current.Request.Files[i];
            f= Init.maxbh().ToString() ;
            string full = fullbasePath + folderdt + "\\" + f + Path.GetExtension(file.FileName);
            string sql = "insert into [ImageDoc](imageid,type_id,leibie,detailid,extend,beactive,url,fzxx5,lastmodifytime)values('" + f + "','pj','注册批件','','" + Path.GetExtension(file.FileName) + "','是','" + basePath.Replace("~", "") + folderdt + "/" + f + Path.GetExtension(file.FileName) + "','" + file.FileName + "',CONVERT(varchar(100), GETDATE(), 120))";
            int success = mssql.ExecuteSql(sql);
            files[0].SaveAs(full);
            var _result = "{\"code\":1,\"msg\":\"\u4e0a\u4f20\u6210\u529f"+f+"|"+file.FileName+"\"}";
            HttpContext.Current.Response.Write(_result);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}